package com.pikaqiu.warehouse.controller;

import com.pikaqiu.warehouse.domain.common.MiniAjaxResult;
import com.pikaqiu.warehouse.domain.dto.CategoryGroupDto;
import com.pikaqiu.warehouse.domain.dto.MaterialProductInfoDto;
import com.pikaqiu.warehouse.domain.dto.MaterialProductLinkInfoDto;
import com.pikaqiu.warehouse.domain.req.MaterialForm;
import com.pikaqiu.warehouse.service.MaterialsService;
import com.pikaqiu.warehouse.utils.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: Administrator
 * @Date: 2024/8/15
 */
@RestController
@RequestMapping("/material")
public class MaterialsController {

    @Resource
    private MaterialsService materialsService;

    @GetMapping("/group/list")
    public MiniAjaxResult<List<CategoryGroupDto>> listGroup() {
        return MiniAjaxResult.success(materialsService.listGroup());
    }

    @GetMapping("/info")
    public MiniAjaxResult<MaterialProductInfoDto> info(Integer id) {
        return MiniAjaxResult.success(materialsService.info(id));
    }

    @GetMapping("/link/info")
    public MiniAjaxResult<MaterialProductLinkInfoDto> linkInfo(Integer id) {
        return MiniAjaxResult.success(materialsService.linkInfo(id));
    }

    @PostMapping("/link/save")
    public MiniAjaxResult<String> saveLinkInfo(Integer id, String links) {
        materialsService.saveLinkInfo(id, links);
        return MiniAjaxResult.success();
    }

    @PostMapping("/save")
    public MiniAjaxResult<String> saveMaterial(MaterialForm materialForm) {
        if (StringUtils.isEmpty(materialForm.getImage())) {
            return MiniAjaxResult.error("请上传图片");
        }
        if (StringUtils.isEmpty(materialForm.getName())) {
            return MiniAjaxResult.error("请填写名称");
        }
        if (materialForm.getCategory() == null) {
            return MiniAjaxResult.error("请选择分类");
        }
        if (StringUtils.isEmpty(materialForm.getCode())) {
            return MiniAjaxResult.error("请填写编码");
        }
        if (materialForm.getStock() == null) {
            return MiniAjaxResult.error("请填写库存");
        }
        if (StringUtils.isEmpty(materialForm.getUnit())) {
            return MiniAjaxResult.error("请选择单位");
        }
        materialsService.saveMaterial(materialForm);
        return MiniAjaxResult.success();
    }
}
